<?php
namespace app\tripapi\model;
use think\Model;
use think\Db;
class TravelPlan extends Model{
	/*
	 * 保存
	 */
	public function TravelPlanSave($params){
        // 定义方法内通用当前时间戳变量
        $output = '';
        $time = time();
        $data['plan_num'] = $params['plan_num'];
        $data['user_id'] = $params['creator_id'];
        $data['department_id'] = $params['department_id'];
        $data['user_name'] = iconv('UTF-8', 'gbk', $params['creator']); // 填报部门名称
        $data['department'] = iconv('UTF-8', 'gbk', $params['department']); // 填报部门名称
        $data['total_day'] = array_sum($params['day_num']); // 出差总天数
        $data['traffic_money'] = array_sum($params['traffic_money']); //交通费总和
        $data['hotel_money'] = array_sum($params['hotel_money']); // 住宿费总和
        $data['meal_money'] = array_sum($params['meal_money']); // 餐饮费总和
        $data['total_money'] = $data['traffic_money'] + $data['hotel_money'] + $data['meal_money']; //费用总和
        $data['creator'] = iconv('UTF-8', 'gbk', $params['creator']); // 填报部门名称;
        $data['creator_id'] = $params['creator_id'];
        $data['create_time'] = $time; // 创建时间
        $approve = Db::name('approval_proces')->where('department_name', '=', iconv('UTF-8', 'GBK', $params['department']))->select();
        if(iconv('GBK', 'UTF-8', $approve['0']['step1']) == $params['creator']){ //以第一审批人为准
            $data['to_user_step'] = $approve['0']['step1']; // 部门领导审批
            $data['to_status_step'] = '1'; // 0 待审批 1 审批通过  2 驳回  3 关闭
            $data['to_uid_step'] = $this->GetApproveListByName($approve['0']['step1']);
            $data['to_user_step1'] = $approve['0']['step2']; // 总经理查阅人姓名
            $data['to_status_step1'] = '0'; // 0 待审批 1 审批通过  2 驳回  3 关闭.
            $data['to_uid_step1'] = $this->GetApproveListByName($approve['0']['step2']); // 总经理id
            $id = Db::name('travel_plan')->insert($data,false, true);
            $output = CustomergetUserIdByUid($this->GetApproveListByName($data['to_user_step1'])).",".'您有一条待审批的月度出差单'.","."erp5/public/tripapi/plan/index?nids=".$id;
        }elseif(iconv('GBK', 'UTF-8', $approve['0']['step2']) == $params['creator']) { //以第二审批人为准
            $data['to_user_step'] = ''; // 部门领导审批
            $data['to_status_step'] = '1'; // 0 待审批 1 审批通过  2 驳回  3 关闭
            $data['to_uid_step'] = '';
            $data['to_user_step1'] = $approve['0']['step2']; // 总经理查阅人姓名
            $data['to_status_step1'] = '1'; // 0 待审批 1 审批通过  2 驳回  3 关闭.
            $data['to_uid_step1'] = $this->GetApproveListByName($approve['0']['step2']); // 总经理id
            $id = Db::name('travel_plan')->insert($data,false, true);
            $output = CustomergetUserIdByUid($this->GetApproveListByName($data['to_user_step1'])).",".'您有一条月度出差单'.","."erp5/public/tripapi/plan/index?nids=".$id;
        }else{
            $data['to_user_step'] = $approve['0']['step1'];
            $data['to_status_step'] = '0'; // 0 待审批 1 审批通过  2 驳回  3 关闭
            $data['to_uid_step'] = $this->GetApproveListByName($data['to_user_step']);
            $id = Db::name('travel_plan')->insert($data,false, true);
            $output = CustomergetUserIdByUid($this->GetApproveListByName($data['to_user_step'])).",".'您有一条待审批的月度出差单'.","."erp5/public/tripapi/plan/index?nids=".$id;
        }
        return array('id' => $id, 'params' => $output);
    }

    /*
     *  更新
     */
    public function TravelPlanUpdate($params){
        // 定义方法内通用当前时间戳变量
        $time = time();
        $data['plan_num'] = $params['plan_num'];
        $data['user_id'] = $params['creator_id'];
        $data['department_id'] = $params['department_id'];
        $data['user_name'] = iconv('UTF-8', 'gbk', $params['creator']); // 填报部门名称
        $data['department'] = iconv('UTF-8', 'gbk', $params['department']); // 填报部门名称
        $data['total_day'] = array_sum($params['day_num']); // 出差总天数
        $data['traffic_money'] = array_sum($params['traffic_money']); //交通费总和
        $data['hotel_money'] = array_sum($params['hotel_money']); // 住宿费总和
        $data['meal_money'] = array_sum($params['meal_money']); // 餐饮费总和
        $data['total_money'] = $data['traffic_money'] + $data['hotel_money'] + $data['meal_money']; //费用总和
        $data['creator'] = iconv('UTF-8', 'gbk', $params['creator']); // 填报部门名称;
        $data['creator_id'] = $params['creator_id'];
        $data['create_time'] = $time; // 创建时间
        //更新 travel plan 总表
        Db::name('travel_plan')->where('plan_num', '=', $params['plan_num'])->update($data);
        return true;
    }
    /*
     *  删除
     */
    public function PlanDelete($id){
        $result = Db::name('travel_plan')->where('id', '=', $id)->delete();
        return $result;
    }
    /*
     * AJax 获取列表页数据
     */
    public function GetApplyList($uid, $data, $nid){
	    //根据当前登录用户的uid 获取跟当前用户相关的申请单
        if($nid == "null "){
            $res = Db::name('travel_plan')->where(function($query) use ($uid) {
                $query->where('creator_id','=',$uid);
            })->whereor(function($query) use ($uid){
                $query->where('to_uid_step','=',$uid);
            })->whereor(function($query) use ($uid){
                $query->where('to_uid_step1','=',$uid);
            })->select();
            $count = count($res);
            $current_page = $data['page'];
            $startSize = "10";
            $lastSize = $startSize*($current_page - 1);
            $result = Db::name('travel_plan')->where(function($query) use ($uid) {
                $query->where('creator_id','=',$uid);
            })->whereor(function($query) use ($uid){
                $query->where('to_uid_step','=',$uid);
            })->whereor(function($query) use ($uid){
                $query->where('to_uid_step1','=',$uid);
            })->order('id','desc')->limit($lastSize,$startSize)->select();
        }else{
            $res = Db::name('travel_plan')->where(function($query) use ($uid) {
                $query->where('creator_id','=',$uid);
            })->whereor(function($query) use ($uid){
                $query->where('to_uid_step','=',$uid);
            })->whereor(function($query) use ($uid){
                $query->where('to_uid_step1','=',$uid);
            })->select();
            $count = count($res);
            $current_page = $data['page'];
            $startSize = "10";
            $lastSize = $startSize*($current_page - 1);
            $result = Db::name('travel_plan')->where(function($query) use ($nid, $uid) {
                $query->where('creator_id','=',$uid)->where('id','=',$nid);
            })->whereor(function($query) use ($nid, $uid){
                $query->where('to_uid_step','=',$uid)->where('id','=',$nid);
            })->whereor(function($query) use ($nid, $uid){
                $query->where('to_uid_step1','=',$uid)->where('id','=',$nid);
            })->order('id','desc')->limit($lastSize,$startSize)->select();
        }

        foreach($result as $key => $value){
            $result[$key]['create_time'] = date('Y-m-d',$value['create_time']);
            $result[$key]['count'] = $count;
        }
        return $result;
    }

    /*
     * 总经办权限 查看所有
     */
    public function GetAllListForManager($data){
        $res = Db::name('travel_plan')->where(array())->select();
        $count = count($res);
        $current_page = $data['page'];
        $startSize = "10";
        $lastSize = $startSize*($current_page - 1);
        $result = Db::name('travel_plan')->where(array())->order('id','desc')->limit($lastSize,$startSize)->select();
        foreach($result as $key => $value){
            $result[$key]['create_time'] = date('Y-m-d',$value['create_time']);
            $result[$key]['count'] = $count;
        }
        return $result;
    }

    /*
     * 获取是否有审批权限
     */
    public function ShowButton($id, $uid){
        $result = Db::name('travel_plan')->where('id', '=', $id)->select();
        if($result['0']['creator_id'] == $uid){
            return false;
        }else{
            if($uid != '13'){
                return true;
            }else{
                return false;
            }
        }
    }

    /*
     * 获取详情
     */
    public function GetDetail($id){
	    $result = Db::name('travel_plan')->where('id', '=', $id)->select();
        foreach($result as $key => $value){
            $result[$key]['department'] = iconv('GBK', 'UTF-8',$value['department']);
            $result[$key]['user_name'] = iconv('GBK','UTF-8',$value['user_name']);
            $result[$key]['create_time'] = date('Y-m-d',$value['create_time']);
            $result[$key]['plan_num'] = $value['plan_num'];
        }
        return $result;
    }
    /*
     * 根据id 获取plan id 为后面的出差申请单列表做准备 依据 出差申请单号 为主键查询
     */
    public function GetPlanId($id){
        $planid = Db::name('travel_plan')->field('id')->where('apply_num', '=', $id)->find();
        return $planid;
    }
    /*
     * 根据id 获取提交人姓名
     */
    public function GetCreateUserById($id){
        $user = Db::name('travel_plan')->where('id', '=', $id)->select();
        $creator_id = $user['0']['creator_id'];
        return $creator_id;
    }
    /*
     * 根据创建人id 获取创建人部门
     */
    public function GetDepartmentBiUid($id){
        $department = Db::name('new_user')->where('uid', '=', $id)->select();
        $department_name = $department['0']['department_name'];
        return $department_name;
    }
    /*
     * 根据创建人id 获取创建人部门
     */
    public function GetUserNameBiUid($id){
        $user = Db::name('new_user')->where('uid', '=', $id)->select();
        $user_name = $user['0']['user_name'];
        return $user_name;
    }
    /*
     *  根据部门名称获取 审批流 选择项
     *  根据审批流 流程走向 更新 travel plan 数据表的审批状态
     */
    public function GetApproveList($nid,$department_name,$uid){
        $approvel = Db::name('travel_plan')->where('id', '=', $nid)->select();
        $s_info = Db::name('approval_proces')->where('department_name', '=', $department_name)->select();
        if($approvel['0']['to_uid_step'] == $uid && $approvel['0']['to_status_step'] == '1'){ // 第一步审批已经通过
            //查找第二步审批人信息
            $userinfo = Db::name('new_user')->where('user_name', '=', $s_info['0']['step2'])->select();
            $data['to_user_step1'] = $s_info['0']['step2'];
            $data['to_status_step1'] = '0';
            $data['to_uid_step1'] = $userinfo['0']['uid'];
            $output = CustomergetUserIdByUid($this->GetApproveListByName($data['to_user_step1'])).",".'您有一条待审批的月度出差单'.","."erp5/public/tripapi/plan/index?nids=".$nid;
        }elseif($approvel['0']['to_uid_step'] == $uid && $approvel['0']['to_status_step1'] == '0'){ // 第一步审批已经通过
            //查找第二步审批人信息
            $userinfo = Db::name('new_user')->where('user_name', '=', $s_info['0']['step2'])->select();
            $userinfo1 = Db::name('new_user')->where('user_name', '=', $s_info['0']['step1'])->select();
            $data['to_user_step'] = $s_info['0']['step1'];
            $data['to_status_step'] = '1';
            $data['to_uid_step'] = $userinfo1['0']['uid'];
            $data['to_user_step1'] = $s_info['0']['step2'];
            $data['to_status_step1'] = '1';
            $data['to_uid_step1'] = $userinfo['0']['uid'];
            $output = CustomergetUserIdByUid($this->GetApproveListByName($data['to_user_step1'])).",".'您有一条月度出差单'.","."erp5/public/tripapi/plan/index?nids=".$nid;
        }else{ // 第一步审批
            $data['to_user_step'] = $approvel['0']['to_user_step'];
            $data['to_status_step'] = '1';
            $data['to_uid_step'] = $uid;
            $data['to_user_step1'] = $s_info['0']['step2'];
            $data['to_status_step1'] = '0';
            $data['to_uid_step1'] = $this->GetApproveListByName($s_info['0']['step2']);
            $output = CustomergetUserIdByUid($this->GetApproveListByName($data['to_user_step1'])).",".'您有一条已审批/待查阅的月度出差单'.","."erp5/public/tripapi/plan/index?nids=".$nid;
        }
        Db::name('travel_plan')->where('id', '=', $nid)->update($data);
        return array('status' => '200', 'params' => $output);
    }
    /*
     * 根据姓名获取审批流流程结果
     */
    public function GetApproveListByName($name){
        $result = Db::name('new_user')->where('user_name', '=', $name)->select();
        return $result['0']['uid'];
    }
    /*
     * 筛选结果
     */
    public function GetApplyListForSearch($uid, $data){
        $res = Db::name('travel_plan')->where('creator_id', '=', $uid)->whereor('to_uid_step', '=', $uid)->whereor('to_uid_step1', '=', $uid)->select();
        $count = count($res);
        $current_page = $data['page'];
        $startSize = "10";
        $lastSize = $startSize*($current_page - 1);
        $result = Db::name('travel_plan')->where('creator_id', '=', $uid)->whereor('to_uid_step', '=', $uid)->whereor('to_uid_step1', '=', $uid)->limit($lastSize,$startSize)->select();
        foreach($result as $key => $value){
            $result[$key]['create_time'] = date('Y-m-d',$value['create_time']);
            $result[$key]['count'] = $count;
        }
        return $result;
    }
}
?>